package servlet.admin;

import entity.Books;
import net.sf.json.JSONObject;
import service.admin.BooksService;
import service.admin.impl.BooksServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * BooksUpdServlet
 *
 * @author 谭文斌
 * @since 2023/9/4
 */
@WebServlet("/admin/booksUpdServlet")
public class BooksUpdServlet extends HttpServlet {
    BooksService service=new BooksServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        String library_id = req.getParameter("library_id");
        String sort_id = req.getParameter("sort_id");
        String position = req.getParameter("position");
        String status = req.getParameter("status");
        String description = req.getParameter("description");
        JSONObject json = new JSONObject();
        PrintWriter out = resp.getWriter();

        Books book=new Books(Integer.valueOf(id),name,author,Integer.valueOf(library_id),Integer.valueOf(sort_id),position,Integer.valueOf(status),description);

        int upd= service.upd(book);

        if(upd==1) {
            json.put("code", "0");
            json.put("msg", "success");
        }else {
            json.put("code", "1");
            json.put("msg", "error");
        }
        out.write(json.toString());
    }
}
